using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ACS.Compiler.Synthes
{
    public class Formatter : IDisposable
    {
        private TextWriter _writer;

        private string _namespace;

        public static Formatter Current;

        public static void CreateCodeFormatter(string moduleName)
        {
            string fileName = moduleName + ".cs";
            Current = new Formatter(fileName);
            Current._namespace = moduleName;
        }
 
        private int offset;

        public string Namespace
        {
            get
            {
                return _namespace;
            }
        }

        public void Incr()
        {
            offset += 4;
        }

        public void Decr()
        {
            offset -= 4;
        }

        public void Format(string text)
        {
            string[] lines = Regex.Split(text, "\n");
            foreach (string s in lines)
            {
                _writer.WriteLine(s);
                _writer.Write(new string(' ', offset));
            }
        }

        public void FormatString(string text)
        {
             _writer.Write(text);
        }

        public Formatter(string fileName)
        {
            _writer = File.CreateText(fileName);
        }

        public void Dispose()
        {
            _writer.Dispose();
        }
    }
}
